<template>
  <div class="feedback-container">
    <div class="header">
      <div class="left">
        <h2>我的反馈</h2>
      </div>
      <div class="right">
        <el-button type="primary" @click="handleAdd">
          提交反馈
        </el-button>
      </div>
    </div>

    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="feedbacks"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="title" label="标题" min-width="150" />
        <el-table-column prop="content" label="内容" min-width="200" show-overflow-tooltip />
        <el-table-column label="反馈时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="handled" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.handled ? 'success' : 'warning'">
              {{ row.handled ? '已处理' : '未处理' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="replyContent" label="回复内容" min-width="200" show-overflow-tooltip>
          <template #default="{ row }">
            {{ row.replyContent || '暂无回复' }}
          </template>
        </el-table-column>
        <el-table-column label="回复人" width="120">
          <template #default="{ row }">
            {{ row.replier?.name || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="回复时间" width="180">
          <template #default="{ row }">
            {{ row.replyTime ? formatDate(row.replyTime) : '-' }}
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 提交反馈对话框 -->
    <el-dialog
      title="提交反馈"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="form.title"
            placeholder="请输入标题"
          />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            :rows="4"
            placeholder="请输入反馈内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button
          type="primary"
          :loading="submitting"
          @click="handleSubmit"
        >
          提交
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'StudentFeedback'
}
</script>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getMyFeedbacks, submitFeedback } from '@/api/student/feedback'
import { formatDate } from '@/utils/format'

// 表格数据
const loading = ref(false)
const feedbacks = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)

// 获取反馈列表
const fetchFeedbacks = async () => {
  try {
    loading.value = true
    const response = await getMyFeedbacks({
      page: currentPage.value - 1,
      size: pageSize.value
    })
    
    if (response) {
      feedbacks.value = response.content
      total.value = response.totalElements
    }
  } catch (error) {
    console.error('Failed to fetch feedbacks:', error)
    ElMessage.error('获取反馈列表失败')
  } finally {
    loading.value = false
  }
}

// 处理分页
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchFeedbacks()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchFeedbacks()
}

// 提交相关
const dialogVisible = ref(false)
const formRef = ref(null)
const submitting = ref(false)

const form = ref({
  title: '',
  content: ''
})

const rules = {
  title: [
    { required: true, message: '请输入标题', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '请输入反馈内容', trigger: 'blur' }
  ]
}

const handleAdd = () => {
  form.value = {
    title: '',
    content: ''
  }
  dialogVisible.value = true
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    submitting.value = true
    
    await submitFeedback(form.value)
    ElMessage.success('提交成功')
    dialogVisible.value = false
    fetchFeedbacks()
  } catch (error) {
    console.error('Submit failed:', error)
    ElMessage.error(error.response?.data || '提交失败')
  } finally {
    submitting.value = false
  }
}

// 在组件挂载时获取数据
onMounted(() => {
  fetchFeedbacks()
})
</script>

<style scoped>
.feedback-container {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  padding: 16px 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.left {
  display: flex;
  align-items: center;
  gap: 16px;

  h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: #303133;
  }
}

.right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.table-card {
  margin-bottom: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination {
  margin-top: 20px;
  padding: 16px 20px;
  display: flex;
  justify-content: flex-end;
  background-color: #fff;
  border-top: 1px solid #ebeef5;
}

/* 调整表格内容垂直对齐 */
:deep(.el-table .cell) {
  display: flex;
  align-items: center;
}
</style> 